package com.first.module.acm.model;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;

import com.first.core.web.model.BaseModel;
import com.google.gson.annotations.Expose;

public class AppUser extends BaseModel implements UserDetails {
	public static String SYSTEM_USER = "";

	public static String SUPER_USER = "";

	@Expose
	protected String userId;

	@Expose
	// 登录账号
	protected String username;

	@Expose
	// 名称
	protected String fullname;

	// 登录密码
	protected String password;

	@Expose
	protected String email;

	@Expose
	protected String position;

	@Expose
	// 办公室电话
	protected String phone;

	@Expose
	// 移动电话
	protected String mobile;

	@Expose
	// 传真
	protected String fax;

	@Expose
	// 通讯住址
	protected String address;

	@Expose
	// 邮编
	protected String zip;

	// @Expose
	// protected Date accessionTime;

	@Expose
	// 用户状态：1表示正常，0表示禁用
	protected Short status;

	@Expose
	protected Short delFlag;

	// 用户类型
	// 1：服务中心；2：媒体
	@Expose
	protected String userType;
	
	@Expose
	//服务中心或媒体编号
	protected String codeNum;

	private Set<AppRole> roles;
	private Set<String> rights = new HashSet();

	public Set<String> getRights() {
		return this.rights;
	}

	public String getFunctionRights() {
		StringBuffer sb = new StringBuffer();

		Iterator it = this.rights.iterator();

		while (it.hasNext()) {
			sb.append((String) it.next()).append(",");
		}

		if (this.rights.size() > 0) {
			sb.deleteCharAt(sb.length() - 1);
		}

		return sb.toString();
	}

	public void setRights(Set<String> rights) {
		this.rights = rights;
	}

	public AppUser() {
	}

	public AppUser(String in_userId) {
		setUserId(in_userId);
	}

	public String getUserId() {
		return this.userId;
	}

	public void setUserId(String aValue) {
		this.userId = aValue;
	}

	public String getUsername() {
		return this.username;
	}

	public void setUsername(String aValue) {
		this.username = aValue;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String aValue) {
		this.password = aValue;
	}

	public String getEmail() {
		return this.email;
	}

	public void setEmail(String aValue) {
		this.email = aValue;
	}

	public String getPosition() {
		return this.position;
	}

	public void setPosition(String aValue) {
		this.position = aValue;
	}

	public String getPhone() {
		return this.phone;
	}

	public void setPhone(String aValue) {
		this.phone = aValue;
	}

	public String getMobile() {
		return this.mobile;
	}

	public void setMobile(String aValue) {
		this.mobile = aValue;
	}

	public String getFax() {
		return this.fax;
	}

	public void setFax(String aValue) {
		this.fax = aValue;
	}

	public String getAddress() {
		return this.address;
	}

	public void setAddress(String aValue) {
		this.address = aValue;
	}

	public String getZip() {
		return this.zip;
	}

	public void setZip(String aValue) {
		this.zip = aValue;
	}

	public Short getStatus() {
		return this.status;
	}

	public void setStatus(Short aValue) {
		this.status = aValue;
	}

	public String getFullname() {
		return this.fullname;
	}

	public void setFullname(String aValue) {
		this.fullname = aValue;
	}

	public Short getDelFlag() {
		return this.delFlag;
	}

	public void setDelFlag(Short delFlag) {
		this.delFlag = delFlag;
	}

	public String getFirstKeyColumnName() {
		return "userId";
	}

	public Set<AppRole> getRoles() {
		return this.roles;
	}

	public void setRoles(Set<AppRole> roles) {
		this.roles = roles;
	}

	public GrantedAuthority[] getAuthorities() {
		GrantedAuthority[] rights = (GrantedAuthority[]) this.roles
				.toArray(new GrantedAuthority[this.roles.size() + 1]);
		rights[(rights.length - 1)] = new GrantedAuthorityImpl("ROLE_PUBLIC");
		return rights;
	}

	public boolean isAccountNonExpired() {
		return true;
	}

	public boolean isAccountNonLocked() {
		return true;
	}

	public boolean isCredentialsNonExpired() {
		return true;
	}

	public boolean isEnabled() {
		return this.status.shortValue() == 1;
	}

	public String getId() {
		return this.userId.toString();
	}

	public String getBusinessEmail() {
		return this.email;
	}

	public String getFamilyName() {
		return this.fullname;
	}

	public String getGivenName() {
		return this.fullname;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public String getCodeNum() {
		return codeNum;
	}

	public void setCodeNum(String codeNum) {
		this.codeNum = codeNum;
	}
	
}
